<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:p="http://primefaces.org/ui"
                template="../templates/principal.xhtml">

    <ui:define name="content" id="content">
        <h:form id="frmMoeda">
            <p:panel id="panel" header="Bem Vindo ao Cadastro de Moedas">
                <p:growl id="messages" life="5000" />

                <p:dataTable id="tabMoeda" widgetVar="wtabMoeda" var="moeda" value="#{moedaBean.lazyModel}" filterEvent="enter"
                             lazy="true" paginator="true" rows="12" paginatorPosition="top" emptyMessage="Nenhum Registro Retornado ...">

                    <p:column headerText="Descrição" sortBy="#{moeda.nomMoeda}" filterBy="#{moeda.nomMoeda}">
                        <h:outputText value="#{moeda.nomMoeda}" />
                    </p:column>

                    <p:column headerText="Sigla" sortBy="#{moeda.sigla}" filterBy="#{moeda.sigla}">
                        <h:outputText value="#{moeda.sigla}" />
                    </p:column>

                    <p:column headerText="Cotação" sortBy="#{moeda.cotacao}" filterBy="#{moeda.cotacao}" style="text-align: right;">
                        <h:outputText value="#{moeda.cotacao}" >
                            <f:convertNumber locale="pt_BR" pattern="#,##0.00" minFractionDigits="2" />
                        </h:outputText>
                    </p:column>

                    <p:column id="date" headerText="Data Cotação" filterBy="#{moeda.dataCotacao}" sortBy="#{moeda.dataCotacao}" style="text-align: center;">
                        <h:outputText value="#{moeda.dataCotacao}">
                            <f:convertDateTime type="date" dateStyle="short" pattern="dd/MM/yyyy"/>
                        </h:outputText>
                    </p:column>

                    <p:column style="width: 100px;">
                        <h:panelGrid columns="4" styleClass="actions" cellpadding="0">
                            <p:commandButton id="selectButton" update=":frmMoeda:panel :frmMoeda:messages" 
                                             icon="ui-icon-search" title="Visualizar // Alterar Moeda" oncomplete="PF('moedaDialog').show()">
                                <f:setPropertyActionListener value="#{moeda}" target="#{moedaBean.selectedMoeda}" />
                                <f:setPropertyActionListener value="#{false}" target="#{moedaBean.salvar}" />
                            </p:commandButton>

                            <p:commandButton id="excluirButton" update=":frmMoeda:panel :frmMoeda:messages" 
                                             icon="ui-icon-trash" title="Excluir Moeda" oncomplete="PF('confirmation').show()">
                                <f:setPropertyActionListener value="#{moeda}" target="#{moedaBean.selectedMoeda}" />
                            </p:commandButton>
                        </h:panelGrid>
                    </p:column>

                    <f:facet name="footer">
                        <div style="text-align: left;">
                            <p:commandButton id="incluirButton" actionListener="#{moedaBean.preparaInserir}" 
                                             update=":frmMoeda:panel :frmMoeda:messages" value="Nova Moeda"
                                             icon="ui-icon-pencil" title="Cadastrar Nova Moeda" oncomplete="PF('moedaDialog').show()" />
                        </div>
                    </f:facet>

                </p:dataTable>

                <p:dialog id="dialog" header="Detalhes Moeda" widgetVar="moedaDialog" resizable="true" width="700" showEffect="explode" hideEffect="explode">

                    <h:panelGrid id="display" columns="2" style="margin-bottom:50px; text-align: left" cellpadding="5">
                        <h:outputText value="Descrição:" />
                        <p:inputText id="innomMoeda" value="#{moedaBean.selectedMoeda.nomMoeda}" style="width:500px; font-weight:bold; " />

                        <h:outputText value="Sigla:" />
                        <p:inputText id="inSigla" value="#{moedaBean.selectedMoeda.sigla}" style="width:100px; font-weight:bold; " />

                        <h:outputText value="Cotação:" />
                        <p:inputText id="inVlrCotacao" value="#{moedaBean.selectedMoeda.cotacao}" style="width:200px; font-weight:bold; text-align: right;"
                                     onkeyup="javascript:maskIt(this,event,'###.###.###,##',true)">
                            <f:convertNumber locale="pt_BR" pattern="###,###,##0.00" minFractionDigits="2" />
                        </p:inputText>

                        <h:outputText value="Data Cotação:" />
                        <p:inputMask id="imDatAbert" value="#{moedaBean.selectedMoeda.dataCotacao}" mask="99/99/9999" style="font-weight:bold; ">
                            <f:convertDateTime type="date" dateStyle="short" pattern="dd/MM/yyyy" />
                        </p:inputMask>
                        <br />
                        <f:facet name="footer">

                            <p:commandButton value="Salvar" update=":frmMoeda:panel :frmMoeda:messages" rendered="#{moedaBean.salvar}"
                                             actionListener="#{moedaBean.inserir}" oncomplete="PF('moedaDialog').hide()" />
                            <p:commandButton value="Salvar" update=":frmMoeda:panel :frmMoeda:messages" rendered="#{!moedaBean.salvar}"
                                             actionListener="#{moedaBean.alterar}" oncomplete="PF('moedaDialog').hide()" />  
                        </f:facet>
                    </h:panelGrid>

                </p:dialog>

                <p:dialog header="Confirma a Exclusão dessa Moeda?" widgetVar="confirmation">

                    <p:commandButton value="Sim" update=":frmMoeda:panel :frmMoeda:messages" oncomplete="PF('confirmation').hide(), PF('moedaDialog').hide()"
                                     actionListener="#{moedaBean.excluir}" />
                    <p:commandButton value="Não" onclick="PF('confirmation').hide()" type="button" />

                </p:dialog>
            </p:panel>
        </h:form>
    </ui:define>
</ui:composition>